package com.jy.myhome.controller;

import com.google.gson.Gson;
import com.jy.myhome.entity.*;
import com.jy.myhome.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("HouseController")
public class HouseController extends UploadController{
     @Autowired
    HouseService hs;

     //添加用户对指定房源的评价
     @ResponseBody
     @RequestMapping("addComment")
     public Integer addComment(Comment c){
         return hs.addComment(c);
     }

    //查询指定房源评论列表
    @ResponseBody
    @RequestMapping("findComment")
    public List findComment(Integer chouseid){
         return hs.findComment(chouseid);
    }


    //修改房源信息
    @ResponseBody
    @RequestMapping("updateHouse")
    public Integer updateHouse(House house){
        System.out.print(house);
        return hs.updateHouse(house);
    }

    //根据id查询指定房源信息
    @ResponseBody
    @RequestMapping("findHouseOne")
    public House findHouseOne(Integer id){
        return hs.findHouseOne(id);
    }


     //详情页 房源/用户/图片三表联查 传入房源id返回三个表的实现类(HUI)的集合
     @ResponseBody
     @RequestMapping("houseInfo")
     public List houseInfo(Integer id){
         return hs.houseInfo(id);
     }

     //根据房源id删除主信息和多张图片并在服务器中将图片删除deleteById
     @ResponseBody
     @RequestMapping("deleteById")
     public Integer deleteById(HttpServletRequest req,Integer id){
         //在服务器中将图片删除
         List list =hs.houseInfo(id);
         //删除主图
         HUI one=(HUI) list.get(0);
         //先判断图片是否存在再删除
         if(one.getImgsurl()!=null&&one.getImgsurl()!="") {
             removeImg(req,one.getImgurl(),"onehouse");
         }
         //删除多图
         for (int i=0;i<list.size();i++){
             HUI many=(HUI)list.get(i);
             //先判断图片是否存在再删除
             if(many.getImgsurl()!=null&&many.getImgsurl()!=""){
                 removeImg(req,many.getImgsurl(),"manyhouse");
             }

         }

         return hs.deleteById(id); //根据房源id删除主信息和多张图片的数据库信息
     }


    //主页/查询新房和二手房最新发布前4条和随机推荐
    @ResponseBody
    @RequestMapping("findByField")
    public Map findByField(Integer num){
        return hs.findByField(num);
    }

    //分页查询房源
    @ResponseBody
    @RequestMapping("findHouseAll")
    public Map findHouseAll(House h, Integer page){
        System.out.print(h+"as");
        System.out.print(page+"asas");
        //设置下标
        h.setIndex((page-1)*h.getCount());
        return hs.findHouseAll(h);
    }


    //添加房源信息和多张定位图片
    @ResponseBody
    @RequestMapping("addHouse")
    public Integer addHouse(MyData jsondata){
        //System.out.println(jsondata.getJsonData()+"vvv");
        MyJsonObject myjson=new Gson().fromJson(jsondata.getJsonData(), MyJsonObject.class);//反射
        return hs.addHouse(myjson);
    }

}
